/**
 * admin/manifests
 */

module.exports = function(app) {
    // const router = app.router;
    const apiRouter = app.apiRouter;
    const Manifest = app.models.Manifest;

    // 更新 manifests
    apiRouter.post('/manifests', function *() {
        const body = this.request.body;
        const type = body.type;

        if(!body.stage) {
            this.throw(400, new Error('Param stage required'));
        }

        let content = body.content || null;
        try {
            content = JSON.parse(content);
        }
        catch(err) { }

        if(!content) {
            this.throw(400, new Error('Param content required'));
        }

        let manifest = yield Manifest.forge({
            type: type,
            stage: body.stage,
            content: JSON.stringify(content),
            from_ip: this.ip
        })
        .save();

        // clean
        manifest.unset('content');

        this.body = manifest;
    });
};
